#include "SectionLocker.h"


gee::SectionLocker::SectionLocker():m_locked(0)
{
	if(!::InitializeCriticalSectionAndSpinCount(&cs,4000))
		::InitializeCriticalSection(&cs);
}

gee::SectionLocker::~SectionLocker()
{
	if(m_locked>0)
	{
		::EnterCriticalSection(&cs);
		::LeaveCriticalSection(&cs);
	}
	::DeleteCriticalSection(&cs);
}

void gee::SectionLocker::lock()
{
	::EnterCriticalSection(&cs);
	m_locked++;
}

void gee::SectionLocker::unlock()
{
	--m_locked;
	::LeaveCriticalSection(&cs);
}

bool gee::SectionLocker::tryLock()
{
	if(::TryEnterCriticalSection(&cs))
	{
		m_locked++;
		return true;
	}
	return false;
}

bool gee::SectionLocker::locked()
{
	return m_locked> 0;
}